package com.oscar.ehome.utils.webview;


import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * webview显示和加载js数据功能类
 * @author xuejiandong
 *
 */
public class ShowWebView {
	private static String minJsString = "";
	private static String hightChartJsString = "";
	/**
	 * 加载webview
	 * @param webview 控件
	 */
	public static void loadwebview(WebView webview){
		WebSettings webSettings = webview.getSettings();
		// 设置是否接受JavaScript
					webSettings.setJavaScriptEnabled(true);
					webSettings.setDefaultTextEncodingName("utf-8");
					webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
					//支持缩放
		webSettings.setSupportZoom(true);
		webSettings.setBuiltInZoomControls(true);
		webview.setInitialScale(100);
		//支持内容重新布局
		webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
	}
	public static void loadwebview2(WebView webview){
		WebSettings webSettings = webview.getSettings();
		// 设置是否接受JavaScript
					webSettings.setJavaScriptEnabled(true);
					webSettings.setDefaultTextEncodingName("utf-8");
					webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
		webSettings.setSupportZoom(true);
		webSettings.setBuiltInZoomControls(true);
		webview.setInitialScale(200);
		webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
	}
	public static void yanzheng(){
		if (minJsString.equals("")) {
			// 将JS转换成字符串
			minJsString = "javascript:"
					+ getFromAssets("jquery-1.7.1.min.js");
		}

		if (hightChartJsString.equals("")) {
			hightChartJsString = "javascript:"
					+ getFromAssets("FusionCharts.js");
		}
	};
	// 读取放在assets文件夹资源的访问 读取本地js文件
			private static String getFromAssets(String fileName) {
			// TODO Auto-generated method stub
				String Result = "";
				// System.out.println("aaaaaaaaa");
				try {
//					InputStreamReader inputReader = new InputStreamReader(
//							MyApplication.getContext().getResources().getAssets().open(fileName));
//					BufferedReader bufReader = new BufferedReader(inputReader);
//					String line = "";
//					while ((line = bufReader.readLine()) != null)
//						Result += line;
//					// Log.d("Result===", Result);
//					inputReader.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("eee");

				return Result;
		}
/**
 * 加载数据
 * @param url 
 * @param jingbaotongji
 */
	public static void setWebpageView(String url, final WebView jingbaotongji) {
		yanzheng();
		loadUrl(jingbaotongji,url);
		jingbaotongji.setWebViewClient(new WebViewClient(){
			
			

			@Override
			public  void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				// 加载本地js文件
				if (minJsString.equals("")) {
					jingbaotongji.loadUrl("javascript:"
							+ getFromAssets("jquery-1.7.1.min.js"));
				} else {
					jingbaotongji.loadUrl(minJsString);
				}

				if (hightChartJsString.equals("")) {
					jingbaotongji.loadUrl("javascript:"
							+ getFromAssets("FusionCharts.js"));
				} else {
					jingbaotongji.loadUrl(hightChartJsString);
				}

				// 启动服务端读取数据startCharts()
				//vehicle_report_view.loadUrl("javascript:startCharts()");

				// 刷新界面
				view.setVisibility(View.VISIBLE);

			
				//progressBar.setVisibility(android.view.View.GONE);
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
				/*progressBar=new View(VehicleReporrtActivity.this);
				progressBar.setVisibility(android.view.View.VISIBLE);*/
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return  true;
				
			}
		});

	}
		public static void loadUrl(final WebView vehicle_report_view, final String url) {
			
			vehicle_report_view.loadUrl(url);
	
}
}
